package com.wantupai.mvp.framework.base.decoration;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by zhangqing on 2017/12/22.
 */

public class BaseDividerItemDecoration extends RecyclerView.ItemDecoration
{

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private final Context mContext;

    private int mOrientation;

    protected Drawable mDrawable;

    protected int padding;

    public int spacing = 0;


    public BaseDividerItemDecoration(Fragment fragment)
    {
        this(fragment.getActivity());
    }

    public BaseDividerItemDecoration(Context context)
    {
        this(context, LinearLayoutManager.VERTICAL);
    }


    public BaseDividerItemDecoration(Context context, int orientation)
    {
        if (orientation != VERTICAL_LIST && orientation != HORIZONTAL_LIST)
        {
            throw new IllegalArgumentException(orientation + "is not allowed");
        }
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
        mDrawable = typedArray.getDrawable(0);
        typedArray.recycle();
        setOrientation(orientation);
    }

    public void setPadding(float padding)
    {
        this.padding = dpToPx(padding);
    }

    private final void setOrientation(int orientation)
    {
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
    {
        super.onDraw(c, parent, state);
        if (parent.getLayoutManager() instanceof GridLayoutManager)
        {
            drawHorientztion(c, parent, state);
            drawVertical(c, parent, state);
        }
        else
        {
            if (mOrientation == LinearLayoutManager.VERTICAL)
            {
                drawVertical(c, parent, state);
            }
            else
            {
                drawHorientztion(c, parent, state);
            }
        }
    }

    public void drawVertical(Canvas canvas, RecyclerView parent, RecyclerView.State state)
    {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + padding;
            int paddingRight = child.getPaddingRight();
            int paddingLeft = child.getPaddingLeft();
            mDrawable.setBounds(left + paddingLeft, top, right - paddingRight, bottom);
            mDrawable.draw(canvas);
        }
    }

    public void drawHorientztion(Canvas canvas, RecyclerView parent, RecyclerView.State state)
    {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + padding;
            mDrawable.setBounds(left, top, right, bottom);
            mDrawable.draw(canvas);
        }
    }

    public final int getSpanCount(RecyclerView recyclerView)
    {
        int spancount = 1;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            spancount = ((GridLayoutManager) layoutManager).getSpanCount();
        }
        return spancount;
    }


    @Override
    public final void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    {
        super.getItemOffsets(outRect, view, parent, state);
        int itemPosition = parent.getChildAdapterPosition(view);
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
        if (parent.getLayoutManager() instanceof GridLayoutManager)
        {
            if (spanCount == 1)
            {
                if (itemPosition == 0)
                {
                    outRect.top = padding;
                    outRect.bottom = padding / 2;
                }
                else if (itemPosition == childCount - 1)
                {
                    outRect.top = padding / 2;
                }
                else
                {
                    outRect.top = padding / 2;
                    outRect.bottom = padding / 2;
                }
            }
            else
            {
                if (itemPosition / spanCount == 0)
                {//第一行
                    outRect.top = padding;
                    outRect.bottom = spacing / 2;
                }
                else if ((itemPosition) / spanCount == lines)
                {//最后一行
                    outRect.top = spacing / 2;
                }
                else
                {
                    outRect.top = spacing / 2;
                    outRect.bottom = spacing / 2;
                }

                if (itemPosition % spanCount == 0)
                {//第一列
                    outRect.left = padding;
                    outRect.right = spacing / 2;
                }
                else if (itemPosition % spanCount == spanCount - 1)
                {//最后一列
                    outRect.left = spacing / 2;
                    outRect.right = spacing;
                }
                else
                {
                    outRect.left = spacing / 2;
                    outRect.right = spacing / 2;
                }
            }

        }
        else
        {
            if (mOrientation == VERTICAL_LIST)
            {
                if (itemPosition == 0)
                {
                    // outRect.top = padding;
                    outRect.bottom = padding / 2;
                }
                else if (itemPosition == childCount - 1)
                {
                    outRect.top = padding / 2;
                }
                else
                {
                    outRect.top = padding / 2;
                    outRect.bottom = padding / 2;
                }
            }
            else
            {
                if (itemPosition == 0)
                {
                    outRect.left = padding;
                    outRect.right = spacing / 2;
                }
                else if (itemPosition == childCount - 1)
                {
                    outRect.left = spacing / 2;
                    outRect.right = padding;
                }
                else
                {
                    outRect.left = spacing / 2;
                    outRect.right = spacing / 2;
                }
            }
        }
    }

    public int dpToPx(float dip)
    {
        return (int) (dip * mContext.getResources().getDisplayMetrics().density + 0.5f);
    }
}
